<template>
  <div>
    <!-- 查询表单区域 -->
    <el-form :inline="true" :model="para" class="demo-form-inline">
      <el-form-item label="参数ID">
        <el-input v-model="para.id" placeholder="请输入参数编号"></el-input>
      </el-form-item>
      <el-form-item label="参数名称">
        <el-input v-model="para.name" placeholder="请输入参数名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <!-- 添加品牌按钮 -->
        <el-button
          type="primary"
          @click="
            dialogFormVisible = true;
            addOrUpdate = '添加';
          "
          >新增参数</el-button
        >
        <el-button type="warning" @click="$router.go(-1)"
          >返回到模板列表</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="参数ID" width="230" prop="id"></el-table-column>
      <el-table-column
        label="参数名称"
        width="230"
        prop="pname"
      ></el-table-column>
      <el-table-column
        label="参数选项"
        width="300"
        prop="options"
      ></el-table-column>
      <el-table-column
        label="所属模板"
        width="250"
        prop="tname"
      ></el-table-column>
      <el-table-column label="排序" width="280" prop="seq"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.row.id);
              addOrUpdate = '编辑';
            "
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- dialog对话框控件 -->
    <el-dialog
      :title="addOrUpdate + '参数'"
      :visible.sync="dialogFormVisible"
      @close="queryPara = { templateId: para.templateId }"
    >
      <el-form :model="queryPara">
        <el-form-item label="参数名称" :label-width="formLabelWidth">
          <el-input v-model="queryPara.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="参数选项" :label-width="formLabelWidth">
          <el-input
            v-model="queryPara.options"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input v-model="queryPara.seq" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelAddOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 数据接收数组
      tableData: [],
      // 总行数
      total: 0,
      // 选择显示每页多少数据数组
      pageSizes: [10, 20, 30, 40],
      // 页号
      currentPage: 1,
      // 页大小
      pageSize: 10,
      // 查询表单数据
      para: {
        id: "",
        name: ""
      },
      // 对话框展示，默认不展示
      dialogFormVisible: false,
      // 判断对话框是添加还是修改
      addOrUpdate: "",
      // dialog对话框输入框长度
      formLabelWidth: "120px",
      // dialog对话框数据
      queryPara: {
        name: "",
        options: "",
        seq: ""
      }
    };
  },
  methods: {
    // 获取数据的方法
    async getPara() {
      const { data: res } = await this.$http.post(
        `vi/goods/para/pageQueryByAll/${this.currentPage}/${this.pageSize}`,
        this.para
      );
      this.tableData = res.data.pageUtils.rows;
      this.total = res.data.pageUtils.total;
    },
    // 通过Id查询品牌方法
    async handleEdit(id) {
      const { data: res } = await this.$http.get(
        `vi/goods/para/detail/${id}`
      );
      this.queryPara = res.data;
      this.dialogFormVisible = true;
      // 把输入框选项中的 ，号改成 回车符号
      this.queryPara.options = this.queryPara.options.replace(/,/g, "\n");
    },
    // 通过Id删除模板方法
    handleDelete(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete(`vi/goods/para/delete/${id}`);
          this.$message.success("删除成功！");
          // 刷新页面
          this.getPara();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 页大小改变方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getPara();
    },
    // 页号改变方法
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getPara();
    },
    // 查询方法
    onSubmit() {
      this.getPara();
    },
    // 添加或修改模板方法
    async handelAddOrUpdate() {
      // 把输入框选项中回车符号改成，
      this.queryPara.options = this.queryPara.options.replace(/\n/g, ",");
	  if (this.queryPara.id) {
		  const { data: res } = await this.$http.put(`vi/goods/para/update`, this.queryPara)
		  this.$message.success(res.message);
	  } else {
		  const { data: res } = await this.$http.put(`vi/goods/para/add`, this.queryPara)
		  this.$message.success(res.message);
	  }
      this.dialogFormVisible = false;
      this.getPara();
    }
  },
  created() {
    this.para.templateId = this.$route.query.templateId;
    this.queryPara.templateId = this.$route.query.templateId;
    this.getPara();
  }
};
</script>

<style></style>
